package jea.machine.transverseprofileviewer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import jea.machine.db.DBConnect;

public class FrameLayoutQuery extends JInternalFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JPanel panelMain;
	//private JScrollPane mainScrollPane;

	private JLabel lblExtract;
	private JComboBox comboFrom, comboTo;
	private JButton btnExtract;

	private DBConnect db;
	
	public FrameLayoutQuery()
	{
		this.setClosable(true);
		this.setIconifiable(false);
		this.setMaximizable(false);
		// this.setResizable(true);
		this.setTitle("Layout Viewer");
		this.setBorder(new LineBorder(Color.DARK_GRAY, 2));
		this.setLocation(100, 100);
		this.setSize(new Dimension(815, 130));
		this.setVisible(true);
		initPanels();
	}

	private void initPanels()
	{
	    	this.setLayout(new BorderLayout());
	    	panelMain = new JPanel(null);
			//mainScrollPane = new JScrollPane(panelMain);
			//this.add(mainScrollPane);
	
			lblExtract = new JLabel("Extract SPS Vacuum Layout between 2 Positions:");
			lblExtract.setBounds(5, 2, 350, 20);

			db = new DBConnect();		
			comboFrom = new JComboBox(db.getLayoutFromSPSLocation(0));
			comboFrom.setBounds(5, 25, 800, 20);
			comboFrom.addActionListener(this);
			comboTo = new JComboBox(db.getLayoutToSPSPosition(99999));
			comboTo.setBounds(5, 55, 800, 20);
			comboTo.addActionListener(this);
			
			btnExtract = new JButton("Extract Vacuum Layout");
			btnExtract.setActionCommand("extract");
			btnExtract.setBounds(285, 80, 200, 20);
			btnExtract.addActionListener(this);
			
			panelMain.add(lblExtract);
            panelMain.add(comboFrom);
            panelMain.add(comboTo);
            panelMain.add(btnExtract);
            
            this.add(panelMain);
	}
	@Override
	public void actionPerformed(ActionEvent event) 
	{
		if ( event.getActionCommand().equals("extract") )
		{
		   String from[] = comboFrom.getSelectedItem().toString().split(" ");
		   String to[] = comboTo.getSelectedItem().toString().split(" ");
           // Injecte le Table Model dans FrameLayoutTable
		   FrameLayoutTable flt = new FrameLayoutTable(db.getLayoutBetweenSPSPosition(Integer.valueOf(from[0]), Integer.valueOf(to[0])));
		   flt.setVisible(true);
		}
	/*
	 JComboBox combo;
		if ( event.getSource() == comboFrom )
		{
			combo = (JComboBox)event.getSource();
			String from = combo.getSelectedItem().toString();
			System.out.println("CHANGE TO "+ from);
			 comboTo.setModel(new DefaultComboBoxModel(db.getLayoutFromSPSLocation(Integer.valueOf(from))));
		}
		if ( event.getSource() == comboTo )
		{
			combo = (JComboBox)event.getSource();
			String to = combo.getSelectedItem().toString();
			System.out.println("CHANGE TO "+ to);
			 comboFrom.setModel(new DefaultComboBoxModel(db.getLayoutToSPSPosition(Integer.valueOf(to))));
		}		
	 */			
	}
}
